﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace BankSimulation
{
    internal class CreditAccount : Account
    {
        public decimal CreditLimit { get; set; }

        public CreditAccount(int accountNumber, string name, decimal balance, decimal creditLimit)
            : base(accountNumber, name, balance)
        {
            CreditLimit = creditLimit;
        }

        public override void Withdraw(decimal amount)
        {
            decimal availableBalance = Balance + CreditLimit;
            if (amount >= 10000)
            {
                var args = new BigMoneyEventArgs(this, amount);
                OnBigMoneyWithdrawn(args);
            }
            if (amount > availableBalance)
            {
                throw new InsufficientFundsException();
            }

            if (amount > Balance)
            {
                // 如果取款超过余额，减去信用额度
                CreditLimit -= (amount - Balance);
                Balance = 0;
            }
            else
            {
                Balance -= amount;
            }
        }

    }
}
